package com.wobangkj.common.api.crud;

import com.wobangkj.common.model.crud.Collect;
import com.wobangkj.common.service.CollectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/collect")
public class CollectController {
    @Autowired
    private CollectService collectService;

    @PostMapping(value = "/create")
    public Object create(@RequestBody Collect data) {
        // 创建时间
        data.setCreatetime(Timestamp.valueOf(LocalDateTime.now()));
        Long a=Long.parseLong(data.getCollectTextId());
        if(data.getCollectClass()==0){
            collectService.updateJobNumById(a);
            return collectService.create(data);
        }
        else {
            collectService.updateShareNumById(a);
            return collectService.create(data);
        }
    }

    @PostMapping(value = "/findById")
    public Object findById(String collectorId,String collectTextId,String collectClass) {
        return collectService.findById(collectorId,collectTextId,collectClass);
    }


    @PostMapping(value = "/delete")
    public Object delete(String collectorId,String collectTextId,Long collectClass) {
        Long id = collectService.findCollect (collectorId,collectTextId,collectClass.toString());
        if(collectClass==0){
            collectService.deleteJobNumById(id);
            return collectService.delete(id);
        }
        else {
            collectService.deleteShareNumById(id);
            return collectService.delete(id);
        }
    }
}
